//
//  AddContactViewController.m
//  EaseMob
//
//  Created by wwj on 16/5/30.
//  Copyright © 2016年 wfw. All rights reserved.
//

#import "AddContactViewController.h"
#import <SVProgressHUD.h>
#import <Masonry.h>
#import <EMSDK.h>


@interface AddContactViewController ()
@property (weak, nonatomic) UISearchBar *searchBar;
@property (weak, nonatomic) UITextField *textField;
@property (weak, nonatomic) UIButton *addBtn;
@end

@implementation AddContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加朋友";
    self.view.backgroundColor = kBgColor;
    
    [self setupSearchBar];
//    [self setupTextField];
    [self setupAddBtn];
}

- (void)setupSearchBar{
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.placeholder = @"搜索";
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    [self.view addSubview:searchBar];
    self.searchBar = searchBar;
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(64);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
    }];
}

- (void)setupTextField{
    UITextField *textField = [[UITextField alloc] init];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textField];
    self.textField = textField;
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchBar.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.mas_equalTo(80);
    }];
}

- (void)setupAddBtn{
    UIButton *addBtn = [[UIButton alloc] init];
    addBtn.backgroundColor = MyColor(26, 178, 10);
    addBtn.layer.cornerRadius = 4.0;
    [addBtn setTitle:@"添加" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
    [addBtn addTarget:self action:@selector(onClickSarch) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    self.addBtn = addBtn;
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.searchBar.mas_bottom).offset(20);
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(35);
    }];
}

#pragma mark 发送加好友申请
- (void)onClickSarch{
    [self.textField resignFirstResponder];
    [[[NSOperationQueue alloc] init] addOperationWithBlock:^{
       NSString *message = self.searchBar.text;
       EMError *error = [[EMClient sharedClient].contactManager addContact:message message:@"我想加您为好友"];
        if (!error) {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SVProgressHUD showSuccessWithStatus:@"发送成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}

@end
